﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

namespace UI
{
    public class TestBattleSelectPage : MonoBehaviour
    {
        public TestBattlePage page;
        public GameObject itemPrefab;
        public UIGrid grid;

        List<GameObject> items = new List<GameObject>();

        int camp;
        int pos;

        public void Open(int camp, int pos)
        {
            gameObject.SetActive(true);
            this.camp = camp;
            this.pos = pos;

            foreach (var item in Dict.DictDataManager.Instance.tableHeroDao.CacheList)
            {
                GameObject obj = GameObject.Instantiate(itemPrefab) as GameObject;
                obj.transform.SetParent(grid.transform);
                obj.transform.localPosition = Vector3.zero;
                obj.transform.localScale = Vector3.one;
                obj.transform.localEulerAngles = Vector3.zero;
                obj.GetComponent<TestBattleSelectItem>().Init(item.id, OnButtonClick);
                obj.SetActive(true);
                items.Add(obj);
            }

            grid.repositionNow = true;
        }

        public void OnButtonClick(GameObject obj)
        {
            page.OnItemSelect(obj.GetComponent<TestBattleSelectItem>().heroId, camp, pos);
            Close();
        }


        public void Close()
        {
            foreach (var obj in items)
            {
                GameObject.Destroy(obj);
            }
            items.Clear();
            gameObject.SetActive(false);
        }

    }
}